/**
 * $Id: BookChapter.java 24 2011-06-17 13:31:30Z feigme $
 */
package com.iary.domain.model.book.chapter;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import com.iary.domain.model.EntitySupport;

public class BookChapter extends EntitySupport<BookChapter> {

	private static final long serialVersionUID = -1686989310417516741L;

	private BookChapterDesc chapterDesc;
	private BookChapterType type;
	private String path;
	private Double order;
	private Set<BookItem> items = new LinkedHashSet<BookItem>();

	BookChapter() {
		// Needed by Hibernate
	}

	public BookChapter(BookChapterDesc chapterDesc, BookChapterType type,
			String path, Double order) {
		this.chapterDesc = chapterDesc;
		this.type = type;
		this.path = path;
		this.order = order;
	}

	public BookChapter(BookChapterDesc chapterDesc, BookChapterType type,
			Double order, Set<BookItem> items) {
		this.chapterDesc = chapterDesc;
		this.type = type;
		this.order = order;
		this.items = items;
	}

	public void addBookItem(BookItem bookItem) {
		if (!items.contains(bookItem)) {
			items.add(bookItem);
		}
	}

	public void removeBookItem(Long itemId) {
		BookItem bi = null;
		for (BookItem i : items) {
			if (i.getId().equals(itemId)) {
				bi = i;
			}
		}
		items.remove(bi);
	}

	public Iterator<BookItem> getBookItems() {
		return items.iterator();
	}

	public BookItem getBookItem(Long itemId) {
		for (BookItem i : items) {
			if (i.getId().equals(itemId)) {
				return i;
			}
		}
		return null;
	}

	public int getItemCount() {
		return items.size();
	}

	public void setChapterDesc(BookChapterDesc chapterDesc) {
		this.chapterDesc = chapterDesc;
	}

	public void setBookChapterType(BookChapterType type) {
		this.type = type;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public void setOrder(Double order) {
		this.order = order;
	}

	public BookChapterDesc getChapterDesc() {
		return chapterDesc;
	}

	public BookChapterType getType() {
		return type;
	}

	public String getPath() {
		return path;
	}

	public Double getOrder() {
		return order;
	}
}
